package com.async.apkupdate.service;

import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.async.apkupdate.ApkDown;
import com.async.apkupdate.BsdiffUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class UpdateVersionService extends Service implements Runnable {
 	String path;
	private String downLoadUrl;
	boolean  isincrement=false;
	String newPath;
	boolean isStopDown=false;

	@Override
	public void onStart(Intent intent, int startId) {
		if (intent != null && intent.getExtras() != null && intent.getExtras().containsKey("url")) {
			downLoadUrl = intent.getExtras().getString("url");
			if (TextUtils.isEmpty(downLoadUrl)) {
				/*Looper.prepare();
				Toast.makeText(getApplicationContext(), "下载地址不能为空", Toast.LENGTH_SHORT).show();
				Looper.loop();*/
				return;
			}
			path=intent.getExtras().getString("filepath");
			isincrement=intent.getBooleanExtra("increment",false);
			newPath=intent.getStringExtra("newFile");
			new Thread(this).start();
		} else {
		/*	Looper.prepare();
			Toast.makeText(getApplicationContext(), "下载地址出错", Toast.LENGTH_SHORT).show();
			Looper.loop();*/
		}
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		try {
			synchronized (this){
				isStopDown=true;
			}
		}catch (Exception e){

		}
	}

	@Override
	public void run() {
		try {
			sendBroadcast(new Intent().setAction(ApkDown.Action_START));
			URL url = new URL(downLoadUrl);
 			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(5000);
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Charser", "GBK,utf-8;q=0.7,*;q=0.3");
			conn.setRequestProperty("Referer", url.toString());
			InputStream inputStream = conn.getInputStream();
			File file = new File(path);
			long length = conn.getContentLength();
			FileOutputStream outputStream = new FileOutputStream(file);
			byte[] buffer = new byte[1024];
			long downLoadSize = 0;
			int currentSize;
			long lastUpdateTime = System.currentTimeMillis();
			while ((currentSize = inputStream.read(buffer)) > 0&&!isStopDown) {
				downLoadSize += currentSize;
				long currentUpdateTime = System.currentTimeMillis();
				if (currentUpdateTime - lastUpdateTime > 100) {
					Intent  intent=new Intent();
					intent.setAction(ApkDown.Action_CURRENTDOWN);
					intent.putExtra("current", downLoadSize);
					intent.putExtra("total",length);
  					sendBroadcast(intent);
 					lastUpdateTime = currentUpdateTime;
				}
				outputStream.write(buffer, 0, currentSize);
			}
			outputStream.flush();
			inputStream.close();
			outputStream.close();
			conn.disconnect();
			if(!isincrement){
				sendBroadcast(new Intent().setAction(ApkDown.Action_FINISH));
				openFile(file);
			}
			else {
				int i=new BsdiffUtils().patch(BsdiffUtils.extract(getApplication()),newPath,path);
				if(i==0){
					sendBroadcast(new Intent().setAction(ApkDown.Action_FINISH));
					File apkfile=new File(newPath);
					openFile(apkfile);
				}
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

    private void openFile(File file) { 
                    // TODO Auto-generated method stub 
                    Intent intent = new Intent(); 
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), 
                                    "application/vnd.android.package-archive"); 
                    startActivity(intent); 
            }

}
